package com.songqintuan.platform.page;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;

import com.songqintuan.common.bean.Page;
import com.songqintuan.common.page.IPojoPage;
import com.songqintuan.common.page.PojoPage;
import com.songqintuan.common.util.SpringFacesUtil;
import com.songqintuan.model.po.Company;
import com.songqintuan.persist.service.hibernate.ICompanyService;
import com.songqintuan.platform.common.constant.PlatformConstants;
import com.songqintuan.platform.common.constant.PlatformSpringBeanConstants;

public class CompanyPage extends PojoPage<Company, String> implements
		IPojoPage<Company, String>, java.io.Serializable {
	private static final long serialVersionUID = -8106629465344224868L;

	public void getPageDatas() {
		Page<Company, String> curPage = new Page<Company, String>();
		Integer pPageNo = this.getPageNo();
		if (null == pPageNo) {
			pPageNo = new Integer(1);
		}
		Integer pPageSize = this.getPageSize();
		if (null == pPageSize) {
			pPageSize = new Integer(PlatformConstants.DEFAULT_ONE_PAGE_NUMBER);
		}
		curPage = this.getData(pPageNo, pPageSize, this.getParam());
		SelectItem[] tempPageItem = new SelectItem[(int) curPage
				.getTotalPageCount()];
		for (int j = 0; j < tempPageItem.length; j++) {
			tempPageItem[j] = new SelectItem(new Integer(j + 1),
					String.valueOf(j + 1));
		}
		this.setPageItems(tempPageItem);
		this.setPage(curPage);
	}

	public CompanyPage(Integer pageNo, Integer pageSize, Company param) {
		super();
		this.setPageSize(pageSize);
		this.setPageNo(pageNo);
		this.setParam(param);

	}

	// 所有的查询均根据param的值确定
	public Page<Company, String> getData(int pageNo, int pageSize, Company param) {// param
																					// is
																					// statuscode
																					// and
																					// customer
		Page<Company, String> resultPage = new Page<Company, String>();
		ICompanyService<Company, String> companyService = (ICompanyService<Company, String>) SpringFacesUtil
				.getSpringBean(PlatformSpringBeanConstants.COMPANY_SERVICE_BEAN_NAME);
		companyService.setEntityClass(Company.class);
		companyService.setPKClass(String.class);
		Criteria criteria = companyService.createCriteria(null);
		Order order = Order.asc("id");
		List<Order> orderList = new ArrayList<Order>();
		orderList.add(order);
		List<Criterion> filterList = new ArrayList<Criterion>();
		// filterList.add(Restrictions.like("skype", ""));add param
		resultPage = companyService.pagedQuery(pageNo, filterList, orderList);// pagesize																	// value;
		return resultPage;
	}
}
